package br.com.foxinline.manager.pesquisar;

import br.com.foxinline.servico.PrefeituraServico;
import br.com.foxinline.util.Msg;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.primefaces.context.RequestContext;

/**
 *
 * @author nayra
 */
@ManagedBean
@ViewScoped
public class ManagerPesquisarPrefeitura implements Serializable {

    @EJB
    private PrefeituraServico prefeituraServico;
    private Date dataIni;
    private Date dataFim;
    private List<Object[]> results;

    @PostConstruct
    public void init() {
        this.dataIni = null;
        this.dataFim = null;
        this.results = null;
    }

    public void pesquisarRelatorio() {
        results = prefeituraServico.findBoletimArrecadacao(dataIni, dataFim);
        
        if (!results.isEmpty()) {
            RequestContext.getCurrentInstance().execute("imprimirRel.jq.click();");
        } else {
            Msg.messagemWarn("Não há dados!");
        }
    }

    public void exibirRelatorio() {
        HashMap parametros = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        parametros.put("dataIni", null);
        parametros.put("dataFim", sdf.format(Calendar.getInstance().getTime()));

        if (dataIni != null && dataFim != null) {
            parametros.put("dataIni", sdf.format(dataIni));
            parametros.put("dataFim", sdf.format(dataFim));
        }

        prefeituraServico.imprimirBoletimArrecadacao(results, parametros);
    }

    public Date getDataIni() {
        return dataIni;
    }

    public void setDataIni(Date dataIni) {
        this.dataIni = dataIni;
    }

    public Date getDataFim() {
        return dataFim;
    }

    public void setDataFim(Date dataFim) {
        this.dataFim = dataFim;
    }

    public List<Object[]> getResults() {
        return results;
    }

    public void setResults(List<Object[]> results) {
        this.results = results;
    }
}
